<template>
	<view>
		<view style="background-color: #fff;">
			<u-cell    @click="bindWx" title="绑定微信" isLink icon="weixin-fill" :value="getWx"></u-cell>
			<u-cell   @click="bindMobile" title="绑定手机" isLink icon="phone-fill" :value="getMobile" ></u-cell>
			<!-- <u-row style="background-color: #fff;padding: 30rpx 0; ">
				<u--text text='注销账号' color='#ff0000' @click='exitLogin' align='center'></u--text>
			</u-row> -->
		</view>
	</view>
</template>

<script>
	let UserInfo = require('../../../../../service/UserInfo.js'),
		mUserInfo;
	var that
	export default {
		data() {
			return {
				userInfo:{}
			}
		},
		computed:{
			getMobile(){
				 
				if(this.userInfo.mobile){
					return this.userInfo.mobile
				}else{
					return '未绑定'
				}
			},
			getWx(){
				if(this.userInfo.open_id){
					return '已绑定'
				}else{
					return '未绑定'
				}
			}
			
		},
		onLoad() {
			that = this
			mUserInfo = new UserInfo(this.mNetTool)
		},
		onShow() {
			uni.getSystemInfoSync({
				success: function (res) {
						console.log(res.model);
						console.log(res.pixelRatio);
						console.log(res.windowWidth);
						console.log(res.windowHeight);
						console.log(res.language);
						console.log(res.version);
						console.log(res.platform);
					}
			})
			let userInfo = uni.getStorageSync('userInfo')
				mUserInfo = new UserInfo(this.mNetTool)
			if(userInfo){
				this.userInfo = userInfo
			}
		},
		methods: {
			bindWx(){
				if(that.userInfo.open_id) return
				uni.showLoading({
					title: '登陆中'
				})
				uni.getProvider({
					service: 'oauth',
					success: res => {
						console.log('getProvider', res)
						uni.login({
							provider: 'weixin',
							onlyAuthorize: true,
							success: R => {
								console.log('=======login============', R)
								let access_token = R.authResult.access_token
								let openid = R.authResult.openid 
								console.log('=======code============', R.code) 
								mUserInfo.wxlogin({
									code: R.code
								}, res => {
									console.log('-----------Res---------------', res)
									if (res.token) { 
										uni.hideLoading()
										mUserInfo.update({
											id:that.userInfo.id,
											openid:res.rows.open_id
										})
									}
								})
				
							},
							fail(e) {
								uni.hideLoading()
								console.log('登录失败', e)
								uni.$u.toast('验证失败，请下载官方应用')
							}
						})
				
					}
				})
			},
			bindMobile(){
				if(that.userInfo.mobile) return
				uni.navigateTo({
					url:'../../../bindPhone/bindPhone'
				})
			},
		}
	}
</script>

<style>

</style>
